﻿using Finance.API.Write.Application.Command.PayableManagementCommand;
using Finance.Domain;
using Finance.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Finance.API.Write.Controllers
{
    /// <summary>
    /// 应付管理
    /// </summary>
    //[Authorize]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PayableManagementController : ControllerBase
    {
        IMediator mediator;
        ILogger<PayableManagementController> logger;

        public PayableManagementController(IMediator mediator, ILogger<PayableManagementController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 应付管理添加
        /// </summary>
        /// <param name="payable"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> AddPayableManagementCommand(AddPayableManagementCommand request)
        {
            try
            {
                
               return await this.mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("应付管理添加失败" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 应付管理逻辑删除
        /// </summary>
        /// <param name="PayableManagementId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> DelPayableManagementCommand(int PayableManagementId)
        {
            try
            {
                DelPayableManagementCommand request = new DelPayableManagementCommand()
                {
                    PayableManagementId = PayableManagementId
                };
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("应付管理逻辑删除失败" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 应付管理修改
        /// </summary>
        /// <param name="payable"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> UpdPayableManagementCommand(UpdPayableManagementCommand request)
        {
            try
            {
                
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("应付管理修改失败" + ex.Message);
                throw;
            }
        }



    }
}
